/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package MetricsRetriever.RetrieverComponents;

import DataStructures.FileRevisionInformation;
import DataStructures.RevisionInformation;
import General.FileManager;
import General.Globals;
import JavaMetrics.BasicFileInfo;
import JavaMetrics.CommonCounter;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 *
 * @author Manuel
 */
public abstract class RepositoryManager {

     public abstract HashMap<Integer,RevisionInformation> getAllRepositoryData(int r);

     public void countCodeLines(HashMap<Integer, RevisionInformation> revisions, FileManager fm){
         System.out.println(" revisions:"+revisions);
         if(revisions == null)
            return;
        for(Integer i : revisions.keySet()){
            RevisionInformation ri = revisions.get(i);
            ArrayList<String> files = fm.process(ri.getDirectory());
            this.assessMetrics(files, ri);
            System.out.println("assessing metrics "+ri.getRevisionNo()+" under "+ri.getDirectory());

        }

    }

    private void assessMetrics(ArrayList<String> files, RevisionInformation ri) {
        if(files != null){
            HashMap<String, FileRevisionInformation> frimap = new HashMap<String, FileRevisionInformation>();

            for(FileRevisionInformation fri : ri.getFilerevisions()){
                frimap.put(fri.getFilename(), fri);
            }
            ri.setNoOfClasses(files.size());
            CommonCounter counter = new CommonCounter();
            BasicFileInfo fileInfo= null;
            long totalLinesOfCode = 0;
            for (int i = 0;i<files.size();i++){
                File f = new File(files.get(i));
                    fileInfo= counter.countlines(f);
                    File testFile = new File(Globals.DATA_DIRECTORY);
                    String toCut = testFile.getAbsolutePath();
                    String relativePath = f.getAbsolutePath().substring(toCut.length()+1);
                    relativePath = relativePath.substring(relativePath.indexOf("/")+1);
                    relativePath = relativePath.substring(relativePath.indexOf("/")+1);
                    //System.out.println("Counting locs for "+relativePath);
                    int newloc = fileInfo.getFsourcelines();
                    if(frimap.containsKey(relativePath)){
                        frimap.get(relativePath).setLoc(newloc);
                    }
                    else{
                        FileRevisionInformation curr = new FileRevisionInformation();
                        curr = new FileRevisionInformation();
                        curr.setFilename(relativePath);
                        curr.setRevisionNo(ri.getRevisionNo());
                        curr.setLinesAdded(0);
                        curr.setLinesDeleted(0);
                        curr.setLinesModified(0);
                        curr.setModification('N');
                        curr.setLoc(newloc);
                        frimap.put(relativePath,curr);
                    }
                    totalLinesOfCode += newloc;
            }
            ri.setLocs(totalLinesOfCode);
            ri.setFilerevisions(new ArrayList<FileRevisionInformation>(frimap.values()));
        }
    }

}
